package Others.Canal.config;


import Others.Canal.properties.CanalProperties;
import Others.Canal.service.CanalService;
import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.net.InetSocketAddress;

@EnableConfigurationProperties({CanalProperties.class})
@ConditionalOnProperty(prefix = "canal", name = "enabled", havingValue = "true", matchIfMissing = true)
public class CanalConfig {

    @Autowired
    private CanalProperties canalProperties;

    /**
     * 创建Canal连接器，用于全量+增量同步服务
     */
    @Bean("canalConnector")
    public CanalConnector canalConnector() {
        return CanalConnectors.newSingleConnector(
                new InetSocketAddress(canalProperties.getServerIp(), canalProperties.getServerPort()),
                canalProperties.getDestination(), 
                canalProperties.getUsername(), 
                canalProperties.getPassword()
        );
    }

    @Bean
    public CanalService canalFullIncrementSyncService() {
    	return new CanalService();
    }


}